<template>
	<view class="view">
		<view class="top-userinfo">
			<view class="avatar-box">
				<p class="title">头像</p>
				<view class="title-left">
					<image class="avatar" :src="userInfo.photo || '/static/images/ic_img.png'" @click="btnChooseImage()" />
					<image class="more-icon" src="/static/images/ic_next.png "></image>
				</view>
			</view>
			<view class="avatar-box" @click="showPopup(userInfo.name || '默认用户', '昵称')">
				<p class="title">修改用户名</p>
				<view class="avatar">{{ userInfo.name || '默认用户' }}</view>
			</view>
			<view class="avatar-box">
				<p class="title">账号</p>
				<p class="avatar">{{ userInfo.phone }}</p>
			</view>
		</view>
		<view class="introduce">
			<view class="introduce-name">个人介绍</view>
			<textarea placeholder="不要超过200个字哦" maxlength="200" v-model="userInfo.introduction" class="introduce-text"
				@input='textInput'></textarea>
		</view>
		<view class="update-information" @click="btnSave">修改信息</view>

		<uni-popup ref="popup" type="dialog">
			<uni-popup-dialog mode="input" message="成功消息" :duration="2000" :before-close="true" @close="close"
				@confirm="confirm" :title="title" :value="nowValue"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>
<script>
	import {
		getUserInfo,
		uploadFile,
		getEscortInfo,
		updateEscortInfo
	} from '@/apis/request.js';
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog';
	export default {
		components: {
			uniPopupDialog
		},
		data() {
			return {
				showBox: false,
				type: 0,
				input: '',
				nowValue: '', //输入框的值
				title: '', //输入框的标题
				userInfo: {},
				editParams: {} //修改用户信息参数
			};
		},
		async onLoad() {
			//性能优化
			Object.assign(this, this.$options.data());
			//同步加载
			this.getApiUserInfo();
		},
		methods: {
			// 获取用户信息
			getApiUserInfo() {
				getEscortInfo().then(res => {
					// console.log('陪诊师信息',res)
					this.userInfo = res
				})
			},
			//显示对话框
			showPopup(val, title) {
				this.nowValue = val;
				this.title = title;
				this.$refs.popup.open();
			},
			close() {
				this.$refs.popup.close();
			},
			confirm(value, newVal) {
				if (this.title == '昵称') {
					this.userInfo['name'] = newVal;
				}
				// this.btnSave();
				this.$refs.popup.close();
			},
			// 修改用户信息
			btnSave() {
				let that = this;
				this.$api.http(
					'/escort/info/updateEscortInfo',
					'POST',
					this.userInfo,
					res => {
						uni.showToast({
							title: '修改成功',
							icon: '',
							duration: 1000
						});
						that.getApiUserInfo();
					},
					err => {}
				);
			},
			// 选择图片
			btnChooseImage() {
				let that = this;
				uni.chooseImage({
					count: 1,
					sizeType: 'original',
					sourceType: ['camera', 'album'],
					success: res => {
						uploadFile(res.tempFilePaths[0]).then(ress => {
							this.userInfo.photo = ress;
							this.btnSave();
						});
					}
				});
			},
			textInput(e) {
				this.userInfo.introduction = e.detail.value
			}
		}
	};
</script>
<style lang="scss" scoped>
	.view {
		background-color: #f2f2f2ff;
		height: 100%;
		min-height: 100vh;

		.top-userinfo {
			background-color: #fff;
		}

		.bottom-userinfo {
			margin-top: 20rpx;
			background-color: #fff;
		}

		image {
			width: 72rpx;
			height: 72rpx;
			border-radius: 50%;
		}

		.mark {
			position: fixed;
			z-index: 1;
			background-color: rgba(0, 0, 0, 0.5);
			display: flex;
			flex-direction: column;
			justify-content: flex-end;
			width: 100%;
			height: 100%;

			.Box {
				background: #ffffff;
				text-align: center;

				input {
					border: 1rpx solid;
					width: 95%;
					margin: auto;
					height: 66rpx;
					margin-bottom: 25rpx;
				}

				.title {
					margin: 20rpx;
				}

				div {
					width: 100%;
					display: flex;
					align-items: center;
					border-top: 1rpx solid #efeeee;

					p {
						width: 50%;
						height: 80rpx;
						font-size: 28rpx;
						text-align: center;
						line-height: 80rpx;
					}

					p:nth-child(1) {
						color: #999999;
						border-right: 1rpx solid #f1efef;
					}

					p:nth-child(2) {
						color: #dd1d1a;
					}
				}
			}
		}

		.avatar-box {
			padding: 30rpx;
			display: flex;
			align-items: center;
			align-content: center;
			justify-content: space-between;
			background-color: #fff;
			border-bottom: 1rpx solid #f3f3f3;

			.title {
				font-size: 28rpx;
			}

			.title-left {
				display: flex;
				align-items: center;
			}

			.avatar {}

			.more-icon {
				margin-left: 10rpx;
				height: 28rpx;
				width: 16rpx;
			}
		}

		.update-information {
			width: 400rpx;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			border-radius: 44rpx;
			background: #ffffffff;
			margin: 90rpx auto 0 auto;
			font-size: 30rpx;
			color: #ff290b;
		}

		.introduce {
			margin-top: 30rpx;
			padding: 30rpx;
			background-color: #fff;
			border-bottom: 1rpx solid #f3f3f3;

			.introduce-name {
				padding-bottom: 30rpx;
				border-bottom: 1rpx solid #f3f3f3;
			}

			.introduce-text {
				padding-top: 20rpx;
				width: 100%;
			}
		}
	}
</style>